﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace Cinser.Common.Wpf.Controls
{
    /// <summary>
    /// FoldWindow.xaml 的交互逻辑
    /// </summary>
    public partial class FoldWindow : UserControl
    {
        private double _contentHeight = 0;
        public FoldWindow()
        {
            InitializeComponent(); 
            this.Border1.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 114, 159, 219));
            this.Border1.Background = new SolidColorBrush(Color.FromArgb(150, 202, 224, 254));

            this.Border2.BorderBrush = new SolidColorBrush(Color.FromArgb(220, 114, 159, 219));
            this.Border2.Background = new SolidColorBrush(Color.FromArgb(200, 202, 224, 254));

        }
        public object Title
        {
            get { return this.lblTitle.Content; }
            set { this.lblTitle.Content = value; }
        }

        public UIElement UIContent
        {
            get
            {
                if (this.GridContent.Children.Count == 0)
                {
                    return null;
                }
                return this.GridContent.Children[0];
            }
            set
            {
                this.GridContent.Children.Clear();
                this.GridContent.Children.Add(value);
            }
        }

        public int State
        {
            get
            {
                if (this.GridContent.Height == 0)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
        }

        public void Show()
        {
            if (this.Visibility != System.Windows.Visibility.Visible)
            {
                this.Visibility = System.Windows.Visibility.Visible;
            }
            //this.Image1.Source = new BitmapImage(new Uri("/Creatar.WZDMP.UIsSL;component/Images/向右32.png", UriKind.RelativeOrAbsolute));

            Storyboard myStoryboard = new Storyboard();
            DoubleAnimation myDoubleAnimation = new DoubleAnimation();
            myDoubleAnimation.From = 0;
            myDoubleAnimation.To = _contentHeight;
            myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.35));
            myStoryboard.Children.Add(myDoubleAnimation);

            Storyboard.SetTarget(myStoryboard, this.GridContent);
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath("(Grid.Height)"));
            myStoryboard.Begin();
            this.Border1.BorderThickness = new Thickness(1, 1, 1, 1);
        }

        public void Close()
        {
            //this.Image1.Source = new BitmapImage(new Uri("/Creatar.WZDMP.UIsSL;component/Images/向左32.png", UriKind.RelativeOrAbsolute));

            Storyboard myStoryboard = new Storyboard();
            DoubleAnimation myDoubleAnimation = new DoubleAnimation();
            this._contentHeight = this.GridContent.ActualHeight;
            myDoubleAnimation.From = this.GridContent.ActualHeight;
            myDoubleAnimation.To = 0;
            myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.35));
            myStoryboard.Children.Add(myDoubleAnimation);

            Storyboard.SetTarget(myStoryboard, this.GridContent);
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath("(Grid.Height)"));
            myStoryboard.Begin();

            this.Border1.BorderThickness = new Thickness(1, 1, 1, 0);
        }

        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            if (this.GridContent.Height == 0)
            {
                this.Show();
                this.Btn.Content = "收起";
            }
            else
            {
                this.Close();
                this.Btn.Content = "展开";
            }
        }

    }
}
